package com.maxh.controller;

import com.alibaba.fastjson.JSONObject;
import com.auth0.jwt.JWT;
import com.maxh.annotation.AdminLoginToken;
import com.maxh.annotation.TeacherLoginToken;
import com.maxh.annotation.UserLoginToken;
import com.maxh.pojo.Information;
import com.maxh.service.InformationService;
import com.maxh.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
public class InformationController {
    @Autowired
    InformationService informationService;

    /*管理员获取所有选课信息*/
    @AdminLoginToken
    @GetMapping("/training/information/list")
    public Object informationList(){
        List informationList = informationService.getInformationList();
        System.out.println("管理员获取所有选课信息："+informationList);
        if(informationList.size()>0){
            return new ReturnData(StatusCode.REQUEST_SUCCESS,informationList ,"数据信息获取完成！");
        }else{
            return new ReturnData(StatusCode.REQUEST_SUCCESS,informationList,"数据为空！");
        }
    }

    /*根据不同状态查找选课信息*/
    @AdminLoginToken
    @GetMapping("/training/information/search")
    public Object informationStudentList(@RequestParam Map<String,Object> map){
        System.out.println(map);
        if(map==null) return new ReturnError(StatusCode.RETURN_ERROR,"关键字不能为空");
        List informationList = (List) informationService.getInfoStudentList(map);
        System.out.println("获取选课信息："+informationList);
        if(informationList.size()>0){
            return new ReturnData(StatusCode.REQUEST_SUCCESS,informationList ,"数据获取完成！");
        }else{
            return new ReturnData(StatusCode.REQUEST_SUCCESS,informationList,"数据为空！");
        }
    }
    /*管理员同意退课*/
    @AdminLoginToken
    @PostMapping("/training/information/updateStatus")
    public Object informationUpdateStatus(@RequestParam("id") int id){
        System.out.println(id);
        if(id>0){
            Boolean isUpdate =  informationService.updateStatus(id);
            System.out.println("管理员同意退课："+isUpdate);
            if(isUpdate){
                return new ReturnData(StatusCode.REQUEST_SUCCESS ,"退课成功！");
            }else{
                return new ReturnError(StatusCode.RETURN_ERROR,"退课失败");
            }
        }else{
            return new ReturnError(StatusCode.RETURN_ERROR,"ID不能为空");
        }
    }
    /*拒绝状态*/
    @AdminLoginToken
    @PostMapping("/training/information/updateReject")
    public Object inUpdateRejectStatus(@RequestParam("id") int id){
        System.out.println(id);
        if(id>0){
            Boolean isUpdate =  informationService.updateRejectStatus(id);
            System.out.println("管理员拒绝退课："+isUpdate);
            if(isUpdate){
                return new ReturnData(StatusCode.REQUEST_SUCCESS ,"拒绝成功！");
            }else{
                return new ReturnError(StatusCode.RETURN_ERROR,"拒绝失败");
            }
        }else{
            return new ReturnError(StatusCode.RETURN_ERROR,"ID不能为空");
        }
    }
    /*管理员添加课程信息*/
    @AdminLoginToken
    @PostMapping("/training/information/add")
    public Object addInformation(Information information) throws ParseException {
        if((information.getCourseId()+"")==null ||(information.getUserId()+"")==null || (information.getTeacherId()+"")==null){
            return new ReturnError(StatusCode.RETURN_ERROR,"必填信息不能为空");
        }
        int  isInformation = informationService.searchInformationList(information);
        System.out.println("判断学员是否选择了该课程："+isInformation);
        if(isInformation>0){
            return new ReturnData(StatusCode.RETURN_ERROR ,"学员已选择该课程，请勿重新选择！");
        }
        Date createTime = new ReturnUtil().dateTime();
        information.setStatus(1);
        information.setReject(0);
        information.setCreateTime(createTime);
        Boolean  isAddCourse = informationService.addInformationList(information);
        if(isAddCourse){
            List informationList = informationService.getInformationList();
            return new ReturnData(StatusCode.REQUEST_SUCCESS, informationList,"添加成功！");
        }else{
            return new ReturnData(StatusCode.RETURN_ERROR ,"添加失败！");
        }
    }

    //讲师根据课程id获取当前课程学员信息
    @TeacherLoginToken
    @GetMapping("/training/teacher/information/searchByCourseId")
    public Object courseStudent(@RequestParam("courseId") int courseId){
        System.out.println(courseId);
        if(courseId > 0){
            String teacherId = TokenUtil.getTokenTeacherId();
            List courseList = informationService.findStudentByCourseId(teacherId,courseId);
            System.out.println("讲师根据课程id获取当前课程学员信息："+courseList);
            if(courseList.size()>0) return new ReturnData(StatusCode.REQUEST_SUCCESS,courseList ,"后端数据交互正常！");
            else return new ReturnData(StatusCode.REQUEST_SUCCESS,courseList ,"数据为空！");
        }else{
            return new ReturnError(StatusCode.RETURN_ERROR,"课程Id不合法或者为空");
        }
    }

    /*修改选课分数*/
    @TeacherLoginToken
    @PostMapping("/training/teacher/information/updateMark")
    public Object informationUpdateMark(@RequestParam("id") int id,@RequestParam("mark") String mark){
        if(mark==null)  return new ReturnError(StatusCode.RETURN_ERROR,"成绩不能为空");
        if(id>0){
            System.out.println(id);
            Boolean isUpdate =  informationService.updateMark(id,mark);
            System.out.println("获取选课信息："+isUpdate);
            if(isUpdate){
                return new ReturnData(StatusCode.REQUEST_SUCCESS ,"后端数据交互正常！");
            }else{
                return new ReturnError(StatusCode.RETURN_ERROR,"同意退课请求失败");
            }
        }else{
            return new ReturnError(StatusCode.RETURN_ERROR,"Id不合法或者为空");
        }

    }

    /*添加课程信息*/
    @UserLoginToken
    @PostMapping("/training/user/information/add")
    public Object userAddInformation(Information information) throws ParseException {
        if((information.getCourseId()+"")==null  || (information.getTeacherId()+"")==null){
            return new ReturnError(StatusCode.RETURN_ERROR,"必填信息不能为空");
        }
        String userId = TokenUtil.getTokenUserId();
        information.setUserId(Integer.parseInt(userId));
        int  isInformation = informationService.searchStudentInformationList(information);
        System.out.println("判断学员是否报名了该课程："+isInformation);
        if(isInformation>0){
            return new ReturnData(StatusCode.RETURN_ERROR ,"该课程已报名，请勿重新报名!");
        }
        Date createTime = new ReturnUtil().dateTime();
        information.setStatus(1);
        information.setReject(0);
        information.setCreateTime(createTime);
        Boolean  isAddCourse = informationService.addInformationList(information);
        if(isAddCourse){
            return new ReturnData(StatusCode.REQUEST_SUCCESS,"报名成功！");
        }else{
            return new ReturnData(StatusCode.RETURN_ERROR ,"报名失败！");
        }
    }

    /*检查学员是否已经添加课程信息*/
    @UserLoginToken
    @PostMapping("/training/user/information/checkAdd")
    public Object checkUserAddInformation(Information information){
        if(information.getCourseId()+""==null){
            return new ReturnError(StatusCode.RETURN_ERROR,"课程Id不能为空");
        }
        String userId = TokenUtil.getTokenUserId();
        information.setUserId(Integer.parseInt(userId));
        int  isInformation = informationService.searchStudentInformationList(information);
        System.out.println("检查学员是否已经添加课程信息："+isInformation);
        if(isInformation>0){
            return new ReturnData(StatusCode.REQUEST_SUCCESS ,isInformation,"该课程你已经报名，请勿重新报名!");
        }else{
            return new ReturnData(StatusCode.RETURN_ERROR ,isInformation,"该课程还没报名，你可以报名!");
        }
    }

    /*学员获取自己的选课信息*/
    @UserLoginToken
    @GetMapping("/training/information/student/list")
    public Object getInformationStudentList(){
        String userId = TokenUtil.getTokenUserId();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("userId", userId);
        System.out.println(jsonObject);
        List informationList = (List) informationService.getStudentCourseList(jsonObject);
        System.out.println("学员获取自己的选课信息："+informationList);
        if(informationList.size()>0){
            return new ReturnData(StatusCode.REQUEST_SUCCESS,informationList ,"后端数据交互正常！");
        }else{
            return new ReturnData(StatusCode.REQUEST_SUCCESS,informationList,"数据为空");
        }
    }

    /*学员取消申请退课*/
    @UserLoginToken
    @PostMapping("/training/student/information/status/cancel")
    public Object StudentUpdateInfoStatus(@RequestParam("id") int id){
        System.out.println(id);
        if(id+""==null) return new ReturnError(StatusCode.RETURN_ERROR,"Id不能为空");
        Boolean isUpdate =  informationService.cancelUpdateStatus(id);
        System.out.println("学员取消申请退课："+isUpdate);
        if(isUpdate){
            return new ReturnData(StatusCode.REQUEST_SUCCESS ,"取消申请成功！");
        }else{
            return new ReturnError(StatusCode.RETURN_ERROR,"取消申请失败");
        }
    }

    /*学员申请退课*/
    @UserLoginToken
    @PostMapping("/training/student/information/status/update")
    public Object StudentUpdateStatus(@RequestParam("id") int id){
        System.out.println(id);
        if(id+""==null) return new ReturnError(StatusCode.RETURN_ERROR,"Id不能为空");
        Boolean isUpdate =  informationService.updateStudentStatus(id);
        System.out.println("学员申请退课："+isUpdate);
        if(isUpdate){
            return new ReturnData(StatusCode.REQUEST_SUCCESS ,"已申请退课！");
        }else{
            return new ReturnError(StatusCode.RETURN_ERROR,"申请退课失败");
        }
    }
}
